$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'EvaluationTime/Select',
        datatype: "json",
        colModel: [
             { label: '评价时间id', name: 'evaluationTimeId', width: 40,key: true },
             { label: '评价起始时间', name: 'evaluationStartDate', width: 40 },
             { label: '评价结束时间', name: 'evaluationEndDate', width: 40 },
             { label: '评价时间是否有效', name: 'evaluationValid', width: 60, formatter: function(value, options, row){
                return value == 0 ?
                    '<span class="table-btn1 isNot">否</span>' :
                    '<span class="table-btn1 isSure">是</span>';
            }},
         ],
        viewrecords: true,
        height:'100%',
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        // rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            key: null
        },
        showList: true,
        title: null,
        EvaluationTime: {},
        evaluationTimeId: null
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.EvaluationTime = {};
        },
        update: function () {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            $.get(baseURL + "EvaluationTime/Info/"+id, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.EvaluationTime = r.EvaluationTime;
                vm.evaluationTimeId = r.EvaluationTime.evaluationTimeId;
            });
        },
        changeTime: function(){
            this.EvaluationTime.evaluationStartDate = $("#startDate").val();
            this.EvaluationTime.evaluationEndDate = $("#endDate").val();
        },
        del: function () {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "EvaluationTime/Delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        saveOrUpdate: function () {
            $("#aaa").validate(
                {
                    debug:false, //调试模式，即使验证成功也不会跳转到目标页面

                    rules:{//配置验证规则，key就是被验证的dom对象，value就是调用验证的方法(也是json格式)
                        startDate:{
                            required:true
                        },
                        endDate:{
                            required:true
                        },
                        Valid:{
                            required:true
                        }
                    },
                    messages:{
                        startDate:{
                            required:"必填字段"
                        },
                        endDate:{
                            required:"必填字段"
                        },
                        Valid:{
                            required:"必填字段"
                        }
                    },
                    submitHandler:function() {
                        var url = vm.EvaluationTime.evaluationTimeId !=null ? "EvaluationTime/Update" : "EvaluationTime/Insert";
                        $.ajax({
                            type: "POST",
                            url: baseURL + url,
                            contentType: "application/json",
                            data: JSON.stringify(vm.EvaluationTime),
                            success: function(r){
                                if(r.code === 0){
                                    alert('操作成功', function(){
                                        vm.reload();
                                    });
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                });
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{ 'evaluationStartDate': vm.q.key},
                page:page
            }).trigger("reloadGrid");
        }
    }
});